import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.Timer;


public class Game extends JPanel implements ActionListener {
	
	private Timer update = new Timer(35, this);
	private Timer speed = new Timer(60, this);
	private Player p1 = new Player(150, 250, 30, Color.orange);
	private Player p2 = new Player(850 - p1.getRadius(), 250, 30, Color.blue);
	private Flag f1 = new Flag(Color.yellow, 50, 150, 150);
	private Flag f2 = new Flag(Color.cyan, 50, 850 - p1.getRadius(), 150);
	
	public Game() {
		update.start();
		speed.start();
		addKeyListener(kl);
		setFocusable(true);

	}
	
	public void resetGame() {
		setFocusable(true);
		p1.resetPlayer();
		p2.resetPlayer();
		p1.setScore(0);
		p2.setScore(0);
		f1.reset();
		f2.reset();
	}
	
	public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D)g;
        
        //Paint the Flags:
		g2d.setColor(f1.getColor());
		g2d.fillRect(f1.getX(), f1.getY(), f1.getSide(), f1.getSide());
		g2d.setColor(f2.getColor());
		g2d.fillRect(f2.getX(), f2.getY(), f2.getSide(), f2.getSide());
		
		//Paint the Players:
		g2d.setColor(p1.getColor());
		g2d.fillOval(p1.getX(), p1.getY(), p1.getRadius(), p1.getRadius());
		g2d.setColor(p2.getColor());
		g2d.fillOval(p2.getX(), p2.getY(), p2.getRadius(), p2.getRadius());
		
		//Paint the Line in the middle:
		g2d.setColor(Color.black);
		g2d.drawLine(500, 0, 500, 500);
        
        
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == speed) {
			//Player1
			if (p1.yI()) {
				p1.ySpeedInc();
			}
			if (p1.yD()) {
				p1.ySpeedDec();
			}
			if (p1.xI()) {
				p1.xSpeedInc();
			}
			if (p1.xD()) {
				p1.xSpeedDec();
			}
			
			//Player2
			if (p2.yI()) {
				p2.ySpeedInc();
			}
			if (p2.yD()) {
				p2.ySpeedDec();
			}
			if (p2.xI()) {
				p2.xSpeedInc();
			}
			if (p2.xD()) {
				p2.xSpeedDec();
			}
		}
		
		if (e.getSource() == update) {
			if (CatchedFlag(p1, f2)) {}
			if (CatchedFlag(p2, f1)) {}
			if (p1.hasFlag()) {
				f2.setX(p1.getX() - (f2.getSide()/4));
				f2.setY(p1.getY() - (f2.getSide()/4));
			}
			else {
				f2.reset();
			}
			if (p2.hasFlag()) {
				f1.setX(p2.getX() - (f1.getSide()/4));
				f1.setY(p2.getY() - (f1.getSide()/4));
			}
			else {
				f1.reset();
			}
			//If player1 has the flag, and is on player2's side of the field, 
			//check if player2 catches player1:
			if (p1.hasFlag() && p1.getX() > 500 && p2.getX() + p2.getRadius() > f2.getX()
												&& p2.getX() < f2.getX() + f2.getSide()
												&& p2.getY() + p2.getRadius() > f2.getY()
												&& p2.getY() < f2.getY() + f2.getSide()) {
				p1.resetPlayer();
				writeScore();
			}
			if (p2.hasFlag() && p2.getX() < 500 && p1.getX() + p1.getRadius() > f1.getX()
												&& p1.getX() < f1.getX() + f1.getSide()
												&& p1.getY() + p1.getRadius() > f1.getY()
												&& p1.getY() < f1.getY() + f1.getSide()) {
				p2.resetPlayer();
				writeScore();
			}
			
			if (p1.hasFlag() && p1.getX() < 500) {
				p1.incScore();
				writeScore();
				p1.resetPlayer();
				p2.resetPlayer();
			}
			if (p2.hasFlag() && p2.getX() + p2.getRadius() > 500) {
				p2.incScore();
				writeScore();
				p2.resetPlayer();
				p1.resetPlayer();
			}
			p1.updatePlayer();
			p2.updatePlayer();
			repaint();		
		}
	}
	
	private Boolean CatchedFlag(Player p, Flag f) {
		if (p.getX() + p.getRadius() > f.getX() 
							&& p.getX() < f.getX() + f.getSide()
							&& p.getY() + p.getRadius() > f.getY() 
							&& p.getY() < f.getY() + f.getSide()) {
			p.catchFlag(true);
			return true;
		}
		else return false;
	}
	
	public void writeScore() {
		Main.setScore(p1.getScore(), p2.getScore());
	
	}
	
	KeyListener kl = new KeyAdapter() {
		@Override
		public void keyPressed(KeyEvent e) {
			
			//Player 1:
			if (e.getKeyCode() == KeyEvent.VK_W) {
				p1.setYd(true);
			}
			else if (e.getKeyCode() == KeyEvent.VK_S) {
				p1.setYi(true);
			}
			if (e.getKeyCode() == KeyEvent.VK_A) {
				p1.setXd(true);
			}
			else if (e.getKeyCode() == KeyEvent.VK_D) {
				p1.setXi(true);		
			}
			//Player 2:
			if (e.getKeyCode() == KeyEvent.VK_UP) {
				p2.setYd(true);
			}
			else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				p2.setYi(true);
			}
			
			if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				p2.setXd(true);
			}
			else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				p2.setXi(true);
			}
		}
		@Override
		public void keyReleased(KeyEvent e) {
			//Player 1:
			if (e.getKeyCode() == KeyEvent.VK_W) {
				p1.setYd(false);
			}
			else if (e.getKeyCode() == KeyEvent.VK_S) {
				p1.setYi(false);
			}
			if (e.getKeyCode() == KeyEvent.VK_A) {
				p1.setXd(false);
			}
			else if (e.getKeyCode() == KeyEvent.VK_D) {
				p1.setXi(false);		
			}
			//Player 2:
			if (e.getKeyCode() == KeyEvent.VK_UP) {
				p2.setYd(false);
			}
			else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				p2.setYi(false);
			}
			
			if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				p2.setXd(false);
			}
			else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				p2.setXi(false);
			}
		}
	};
}
